﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScript.BLL.FI;

namespace MixScriptWeb.Accounting
{
    public partial class ListJournal : cCommonWeb, IMIxCommon //System.Web.UI.Page
    {
        private static SortDirection sd;
        private static DataTable dt;
        public bool isvalid()
        {
            return true;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlMonth.SelectedIndex = DateTime.Now.Month - 1;
                ddlTahun.SelectedIndex = ((int)DateTime.Now.Year - 2011);

                LoadData();

            }
        }

        private void LoadData()
        {

            cAccHeader gj = new cAccHeader();
            dt = new DataTable();
            int type = Convert.ToInt32(Request.QueryString["type"]);

            if (type == 1)
            {
                lblTitle.Text = "General Journal";
                btnAdd.PostBackUrl = "fJournalGeneral.aspx";
                ViewState["EditURL"] = "fJournalGeneral.aspx?";
            }
            else if (type == 2)
            {
                lblTitle.Text = "Cash Recieve";
                btnAdd.PostBackUrl = "fJournalCash.aspx?type=2";
                ViewState["EditURL"] = "fJournalCash.aspx?type=2&";
            }
            else if (type == 3)
            {
                lblTitle.Text = "Cash Payment";
                btnAdd.PostBackUrl = "fJournalCash.aspx?type=3";
                ViewState["EditURL"] = "fJournalCash.aspx?type=3&";

            }
            else if (type == 4)
            {
                lblTitle.Text = "Account Receivable";
                btnAdd.PostBackUrl = "fAccJournal.aspx?type=4";
                ViewState["EditURL"] = "fAccJournal.aspx?type=4&";

            }
            else if (type == 5)
            {
                lblTitle.Text = "Account Payment";
                btnAdd.PostBackUrl = "fAccJournal.aspx?type=5";
                ViewState["EditURL"] = "fAccJournal.aspx?type=5&";
            }
            //else if (type == 6)
            //{
            //    lblTitle.Text = "Sales Journal";
            //    btnAdd.PostBackUrl = "fSalesJournal.aspx?type=6";
            //    ViewState["EditURL"] = "fSalesJournal.aspx?type=6&";
            //}
            //else if (type == 6)
            //{
            //    lblTitle.Text = "Purchase";
            //    btnAdd.PostBackUrl = "fPurchase.aspx?type=6";
            //    ViewState["EditURL"] = "fPurchase.aspx?type=6&";
            //}
            if (gj.ListJournal(out dt, Convert.ToInt32(ddlTahun.SelectedItem.Value), Convert.ToInt32(ddlMonth.SelectedItem.Value), type))
            {

                bindGridview(gvList, dt);
            }

        }
        protected void btnRefresh_click(Object sender, EventArgs e)
        {
            LoadData();
        }
        protected void gvList_OnRowCreated(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                (e.Row.FindControl("lbEdit") as LinkButton).PostBackUrl = ViewState["EditURL"] + "editID="
                    + DataBinder.Eval(e.Row.DataItem, "slipid");
            }
        }
        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Deleting")
            {
                cAccHeader hdr = new cAccHeader();
                hdr.deleteJournal((string)e.CommandArgument);
            }
            LoadData();

        }
        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvList, dataView);
        }
        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvList.PageIndex = e.NewPageIndex;
            bindGridview(gvList, dt);

        }
        protected void gvList_DataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[9].Text == "1" || e.Row.Cells[11].Text.Replace("&nbsp;", "") != "")
                {
                    (e.Row.FindControl("lbEdit") as LinkButton).Style["Display"] = "none";
                    (e.Row.FindControl("lbDelete") as LinkButton).Style["Display"] = "none";
                }
            }
        }

    }
}